package com.gientech.madp.sm2.controller;

import cn.hutool.json.JSONObject;
import com.gientech.madp.sm2.service.SM2Service;
import com.gientech.madp.sm2.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author smile_ZhongXuan
 * @Date 2021/10/8 11:17
 * @Version 1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sm2")
@Api(value = "api", tags = "密钥签名")
public class SM2Controller {

	private final SM2Service sm2Service;

	/**
	 * 生成SM2密钥
	 * @return
	 */
	@ApiOperation(value = "生成SM2密钥")
	@PostMapping("/generate-key")
	public R generateKey() {
		return sm2Service.generateKey();
	}

	/**
	 * 加签
	 * @param privateKey  私钥
	 * @param jsonObject  加签报文体
	 * @return
	 */
	@ApiOperation(value = "加签")
	@PostMapping("/counter-sign")
	public R counterSign(@RequestHeader String privateKey, @RequestBody JSONObject jsonObject){
		return sm2Service.counterSign(privateKey, jsonObject);
	}

	/**
	 * 验签
	 * @param publicKey  公钥
	 * @param jsonObject  验签报文体
	 * @return
	 */
	@ApiOperation(value = "验签")
	@PostMapping("/verify-signature")
	public R verifySignature(@RequestHeader String publicKey, @RequestBody JSONObject jsonObject){
		return sm2Service.verifySignature(publicKey,jsonObject);
	}

	/**
	 * 校验密钥
	 * @param privateKey  私钥
	 * @param publicKey  公钥
	 * @return
	 */
	@ApiOperation(value = "校验密钥")
	@PostMapping("/verification-key")
	public R verificationKey(@RequestHeader String privateKey, @RequestHeader String publicKey) {
		return sm2Service.verificationKey(privateKey,publicKey);
	}

	/**
	 * 加密
	 * @param publicKey  公钥
	 * @param jsonObject   加密报文体
	 * @return
	 */
	@ApiOperation(value = "加密")
	@PostMapping("/encrypt")
	public R encrypt(@RequestHeader String publicKey, @RequestBody JSONObject jsonObject) {
		return sm2Service.encrypt(publicKey,jsonObject);
	}

	/**
	 * 解密
	 * @param privateKey  私钥
	 * @param jsonObject  加密后的报文
	 * @return
	 */
	@ApiOperation(value = "解密")
	@PostMapping("/decrypt")
	public R decrypt(@RequestHeader String privateKey, @RequestBody JSONObject jsonObject) {
		return sm2Service.decrypt(privateKey,jsonObject);
	}
}
